package client;

import common.Game;
import common.Player;

public class ClientObserverUpdate {
	private final ClientController controller;

	public ClientObserverUpdate(final ClientController g) {
		this.controller = g;
	}

	public void observerUpdate(final Game sg) {

		final Player mePlayer = sg.getPlayer(this.controller
				.getThisPlayerNumber());

		// Set the right player border Color
		this.controller.getGUI().changePlayerBorderColor(
				this.controller.getPlayerColor(this.controller
						.getThisPlayerNumber()));

		// Set the default card
		this.controller.setDefaultCardName(mePlayer.getCurrentCardViewed());
		this.controller.changeGameBoardInfoPanelUpdate(mePlayer
				.getCurrentCardViewed());

		// First, change to the main game panel, in case we still are at the
		// connect screen
		this.controller.getGUI().setConnectPanelVisibility(false);
		this.controller.getGUI().setMainGamePanelVisibility(true);

		// Draw all the players' positions
		this.controller.getMapButtonHandler().hideAllButtons();
		this.controller.getMapButtonHandler().showAllPlayers(
				sg.getPlayerPositions());

		// This updates the map tile list of the GUI.
		this.controller.getGUI().getClientController()
				.setMapTileList(sg.getGameTileList());

		// Set border color for the user
		this.controller.getGUI().changePlayerBorderColor(
				this.controller.getPlayerColor(this.controller
						.getThisPlayerNumber()));

		// This updates the map tile list of the GUI.
		this.controller.setMapTileList(sg.getGameTileList());

		// Set the log book
		this.controller.getGUI().changeLogBook(sg.getLog());

		// Update the turn ended label
		this.controller.getGUI().changeTurnEndedLabel(
				"It is now " + sg.getPlayer(sg.getCurrentPlayer()).getName()
						+ "'s turn.");

		// Character info panel update
		this.controller.getGUI()
				.changePlayerImageLabel(mePlayer.getImageIcon());
		this.controller.getGUI().changePlayerNameLabel(mePlayer.getName());
		this.controller.getGUI().changePlayerLivesLabel(
				"Lives: " + mePlayer.getLives());
		this.controller.getGUI().changePlayerCoinsLabel(
				"Coins: " + mePlayer.getCoins());
		this.controller.getGUI().changePlayerCraftLabel(
				"Craft: " + mePlayer.getCraft() + " (" + mePlayer.getCraftXP()
						+ "/7)");
		this.controller.getGUI().changePlayerStrengthLabel(
				"Strength: " + mePlayer.getStrength() + " ("
						+ mePlayer.getStrengthXP() + "/7)");
		// End character info panel update

		// If a player has won, show the game ended screen.
		if (sg.getPlayerWon() != -1) {
			this.controller.getGUI().changeGameEndedNameLabel(
					sg.getPlayer(sg.getPlayerWon()).getName()
							+ " has won the game!");
			this.controller.getGUI().changeGameEndedImageLabel(
					sg.getPlayer(sg.getPlayerWon()).getImageIcon());
			this.controller.getGUI().changeGameEndedImageLabelBorderColor(
					this.controller.getPlayerColor(sg.getPlayerWon()));
			this.controller.setDefaultCardName("gameEndedCard");
			this.controller.changeGameBoardInfoPanelUpdate("gameEndedCard");

		}

		// At last, repaint the GUI
		this.controller.getGUI().repaint();
	}
}
